package com.css.fxfzcpxx.modules.questionData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzcpxx.base.response.RestResponse;
import com.css.fxfzcpxx.modules.questionData.entity.QuestionData;
import com.css.fxfzcpxx.modules.questionData.param.GetScoreParam;
import com.css.fxfzcpxx.modules.questionData.param.QuestionDataParam;
import com.css.fxfzcpxx.modules.questionData.service.QuestionDataService;
import com.css.fxfzcpxx.util.PlatformPageUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.InputStream;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/03/11
 * @Description: 题库
 */
@Api(tags ="题库")
@Slf4j
@RestController
@RequestMapping("/questionData")
public class QuestionDataController {

    @Resource
    private QuestionDataService questionDataService;

    /**
     * 分页列表
     * @param param 参数
     * @return page
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request, QuestionDataParam param) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = questionDataService.getPage(curPage, pageSize,param);
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 修改单条数据
     * @param param 参数
     * @return
     */
    @PostMapping("/updateById")
    @Operation(summary = "修改单条数据")
    public RestResponse updateById(@RequestBody QuestionData param) {
        try {
            return questionDataService.updateById(param);
        } catch (Exception e) {
            log.error("修改数据失败!", e);
            return RestResponse.fail("修改数据失败!");
        }
    }

    /***
     * 删除
     * @param
     * @return
     */
    @PostMapping("/delete/{ids}")
    public RestResponse delete(@PathVariable("ids") String ids){
        RestResponse response = null;
        try{
            questionDataService.delete(ids);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    /**
     * 根据id查询
     * @param id 主键id
     * @return
     */
    @GetMapping("/findById")
    @Operation(summary = "根据id查询")
    public RestResponse findById(String id) {
        try {
            JSONObject data = questionDataService.findById(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 新增单条数据
     * @param param 参数
     * @return
     */
    @PostMapping("/save")
    @Operation(summary = "新增单条数据")
    public RestResponse save(@RequestBody QuestionData param) {
        try {
            return questionDataService.save(param);
        } catch (Exception e) {
            log.error("新增失败!", e);
            return RestResponse.fail("新增失败!");
        }
    }

    /**
     * 目录下载
     * @param ids
     */
    @Operation(summary = "目录下载")
    @PostMapping("/exportData/{ids}")
    public void exportData(HttpServletResponse response, @PathVariable("ids") String ids) {
        questionDataService.exportData(response, ids);
    }

    /***
     * 导入
     * @return
     */
    @PostMapping("/importExcel")
    public RestResponse reportImport(HttpServletRequest req){
        RestResponse response = null;
        try {
            //获取上传到服务器的临时文件对象
            Part part = req.getPart("file");
            //获取临时文件的输入流
            InputStream inputStream = part.getInputStream();
            String str = questionDataService.reportImport(inputStream);
            if (str.equals("导入成功")){
                response = RestResponse.succeed();
                response.setMessage("导入成功");
            }else {
                response = RestResponse.fail(str);
            }
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 根据课堂id生成试卷
     * @param id 主键id
     * @return
     */
    @GetMapping("/getTestById")
    @Operation(summary = "根据课堂id生成试卷")
    public RestResponse getTestById(String id) {
        try {
            RestResponse data = questionDataService.getTestById(id);
            return data;
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /***
     * 判卷
     * @param
     * @return
     */
    @PostMapping("/getScore")
    @Operation(summary = "判卷")
    public RestResponse getScore(@RequestBody GetScoreParam getScoreParam){
        try{
            RestResponse restResponse= questionDataService.getScore(getScoreParam);
            return restResponse;
        }catch (Exception e){
            String errorMessage = "判卷失败!";
            log.error(errorMessage,e);
            return RestResponse.fail(errorMessage);
        }
    }
}
